<template>
  <div class="invoiceDialog">
    <div class="main">
      <el-form ref="form" :model="invoiceInfo" label-width="80px">
        <el-form-item label="总数量">
          <el-input readonly v-model="invoiceInfo.count"></el-input>
        </el-form-item>
        <el-form-item label="总重量">
          <el-input readonly v-model="invoiceInfo.weight">
            <template slot="append">吨</template>
          </el-input>
        </el-form-item>
        <el-form-item label="总金额">
          <el-input readonly v-model="invoiceInfo.money">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="含税金额">
          <el-input readonly v-model="invoiceInfo.money_bill">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="税号">
          <el-input readonly v-model="invoiceInfo.sh">
          </el-input>
        </el-form-item>
        <el-form-item>
          <!-- <el-button type="primary" @click="transfer">转账</el-button> -->
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" class="btn" @click="invoice">开票</el-button>

  </div>
</template>

<script>
import { applicationInvoices } from "@/api/childCompany/moneyManagment.js";
import Big from "big.js";
export default {
  name: "invoiceDialog",
  props: {
    rows: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      invoiceInfo: {
        count: 0,
        weight: 0,
        money: 0,
        sh: "", //税号
        money_bill:0,//含税金额
      }
    };
  },
  mounted() {
    this.initial();
  },
  methods: {
    async initial() {
      this.invoiceInfo.count = this.rows.length;
      this.invoiceInfo.sh = this.rows[0].shxydm;
      this.invoiceInfo.company = this.rows[0].company_name;
      this.invoiceInfo.weight = this.rows.reduce((p, c) => {
        let kg = new Big(c.kg);
        let _p = new Big(p);
        return _p.plus(kg).toNumber();
      }, 0);
      this.invoiceInfo.money = this.rows.reduce((p, c) => {
        let price = new Big(c.price);
        let _p = new Big(p);
        return _p.plus(price).toNumber();
      }, 0);
      this.invoiceInfo.money_bill = this.rows.reduce((p, c) => {
        let money_bill = new Big(c.money_bill);
        let _p = new Big(p);
        return _p.plus(money_bill).toNumber();
      }, 0);
    },
    async invoice() {
      let ids = this.rows.map(item => {
        return item.id;
      });
      let result = await applicationInvoices({
        id: ids.join(","),
        money: this.invoiceInfo.money,
        number: this.invoiceInfo.count,
        weight: this.invoiceInfo.weight,
        company: this.invoiceInfo.company,
        tax: this.invoiceInfo.sh,
        money_bill: this.invoiceInfo.money_bill,
      });
      if (result.code == 200) {
        this.$message.success(result.msg);
        this.$emit('closeDialog');
      }
    }
  }
};
</script>

<style lang="scss" scope>
.invoiceDialog {
  .btn {
    width: 150px;
    margin-left: 50%;
    transform: translateX(-50%);
    margin-top: 20px;
  }

  .main {
    width: 70%;
    margin: 0 auto;
    font-size: 16px;
    // background: pink;
    display: flex;
    flex-direction: column;

    .item {
      display: flex;
      margin-bottom: 20px;

      .title {
        width: 120px;
        text-align: right;
      }

      .content {
        margin-left: 10px;
      }
    }
  }
}
</style>
